//<p>给定一个二叉树的 <strong>根节点</strong> <code>root</code>，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。</p>
//
//<p> </p>
//
//<p><strong>示例 1:</strong></p>
//
//<p><img src="https://assets.leetcode.com/uploads/2021/02/14/tree.jpg" style="width: 270px; " /></p>
//
//<pre>
//<strong>输入:</strong> [1,2,3,null,5,null,4]
//<strong>输出:</strong> [1,3,4]
//</pre>
//
//<p><strong>示例 2:</strong></p>
//
//<pre>
//<strong>输入:</strong> [1,null,3]
//<strong>输出:</strong> [1,3]
//</pre>
//
//<p><strong>示例 3:</strong></p>
//
//<pre>
//<strong>输入:</strong> []
//<strong>输出:</strong> []
//</pre>
//
//<p> </p>
//
//<p><strong>提示:</strong></p>
//
//<ul>
//	<li>二叉树的节点个数的范围是 <code>[0,100]</code></li>
//	<li><meta charset="UTF-8" /><code>-100 <= Node.val <= 100</code> </li>
//</ul>
//<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 607</li><li>👎 0</li></div>
  
package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _199_BinaryTreeRightSideView{
    public static void main(String[] args) {
         Solution solution = new _199_BinaryTreeRightSideView().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        return dfs(result, root, 0);
    }

    private List<Integer> dfs(List<Integer> result, TreeNode root, int i) {
        if (root == null) {
            return result;
        }
        if (i == result.size()){
            result.add(root.val);
        }
        i++;
        dfs(result, root.right, i);
        dfs(result, root.left, i);
        return result;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}